import { createApp } from 'vue';
import App from './App.vue';
import '@/assets/less/index.less';
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
import "@/api/mock.js";
// 正确导入：只导入createRouter，不导入router
import { createRouter, createWebHashHistory } from 'vue-router';

// 从路由配置文件导入已创建的router实例（推荐方式）
import router from './router';
import { createPinia } from 'pinia'
import api from '@/api/api'
import {} from '@/stores'
import { useAllDataStore } from './stores';

function isRoute(to){
   return router.getRoutes().filter(item=>item.path===to.path).length>0
}

router.beforeEach((to, from) => {
     //如果要跳转的不是login,且token不存在(可以通过不存在token判断出用户未登录)
    if(to.path !== '/login'&&!store.state.token){
        //跳转到login
        return { name: 'login' }
    }
    //如果路由记录不存在
    if(!isRoute(to)){
        //跳转到404界面
        return {name: "404"}
    }
})
const pinia = createPinia()
const app = createApp(App);

// 注册图标组件
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
app.config.globalProperties.$api = api
app.use(pinia);
const store=useAllDataStore();



store.addMenu(router,"refresh");
// 在main.js调用addMenu后添加日志
store.addMenu(router, "refresh");
console.log(`[main.js] addMenu执行后的token值：${store.state.token}（类型：${typeof store.state.token}）`);
app.use(ElementPlus);
app.use(router); // 使用导入的router实例
app.mount('#app');
